export const sleep = (time) => {
    return new Promise((resolve) => setTimeout(resolve, time));
}

export const formatDuration = (time) => {
    const duration = +time
    if (!duration) return '---'
    let h = Math.floor(duration / 3600)
    let m = Math.floor(duration % 3600 / 60)
    let s = Math.floor(duration % 3600 % 60)

    if (h > 0) {
        h = h >= 10 ? h : '0' + h
    }
    m = m >= 10 ? m : '0' + m
    s = s >= 10 ? s : '0' + s

    return h ? `${h}:${m}:${s}` : `${m}:${s}`

}

export const isValidArray = (val) => {
    return Array.isArray(val) && val.length > 0
}

export const throttle =(func, delay) =>{
    let flag = true;
    return function () {
        if (flag) {
            flag = false;
            func.apply(this, arguments);
            setTimeout(() => {
                flag = true;
            }, delay);
        }
    }
}

export const getValidProp = (obj)=>{
    let res = {}
    Object.keys(obj).map(key=>{
        if(key !== 'src'){
           if(Object.prototype.toString.call(obj[key]) === '[object Object]') {
              res =  {...res,...{
                [key]:getValidProp(obj[key])
              }}
           } else {
                res[key] = obj[key]
           }     
        }
    })
    return res 
}
